package cn.edu.scau.cmi.linzhiyang.composite.consistent;

import java.util.ArrayList;
import java.util.Iterator;

public class Organization extends Component{
	
	String name;
	ArrayList<Component> list = new ArrayList<Component>();
	
	public Organization() {
		
	}
	
	public Organization(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public ArrayList<Component> getList(){
		return list;
	}
	
	public boolean addMember(Component c) {
		if(c != null) {
			list.add(c);
			return true; 
		}
		else return false;
	}
	
	public boolean delMember(Component c) {
		Component com = null;
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getName().equals(c.getName())) {
				com = list.get(i);
			}
		}
		if(com!=null) {
			list.remove(com);
			return true;
		}
		else return false;
	}
	
	public String getMembers() {
		String str = "组织——"+getName()+"有以下成员:\n";
		Iterator it = list.iterator();
		while(it.hasNext()) {
			Component c = (Component) it.next();
			if(c instanceof Member) {
				str = str + "\t" + "个人：" + c.getName() + "\n"; 
			}
			else if(c instanceof Organization) {
				str = str + "\t" + "组织：" + c.getName() + "\n"; 
			}
		}
		return str;
	}

}